<xsl:stylesheet version='1.0'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au"
  xmlns:incentive='http://schema.pbs.gov.au/Incentive#'
  xmlns:terms="http://purl.org/dc/terms/"
  xmlns:elements="http://purl.org/dc/elements/1.1/">

  <db:article>
    <db:info>
      <db:title>Format Dispensing Incentive Items</db:title>

      <db:copyright>
        <db:year>2011</db:year>
        <db:year>2008</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").  The following terms apply to all files associated with the software unless explicitly disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this software and its documentation for any purpose, provided that existing copyright notices are retained in all copies and that this notice is included verbatim in any distributions. No written agreement, license, or royalty fee is required for any of the authorized uses. Modifications to this software may be copyrighted by their authors and need not follow the licensing terms described here, provided that the new terms are clearly indicated on the first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>1.2</db:revnumber>
          <db:date>2011-06-24</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Updated for PBS XML v2.5.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.1</db:revnumber>
          <db:date>2011-01-21</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Updated for PBS XML v2.4.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2008-06-11</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version, based on extract-dispensing-incentive.xsl.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>
  </db:article>

  <xsl:output method='html' indent='yes'/>

  <db:param>What information to display, and the order in which to display it. Allowed values are: <db:literal>drug-name</db:literal>, <db:literal>item-code</db:literal>, <db:literal>org-code</db:literal> and <db:literal>amount</db:literal>.</db:param>
  <xsl:param name='display'>
drug-name
item-code
org-code
amount
  </xsl:param>

  <db:param>What mode to operate in. Allowed values are: <db:literal>auto</db:literal>, <db:literal>full</db:literal> or <db:literal>update</db:literal>. <db:literal>full</db:literal> mode (the default) produces a full list of brands. <db:literal>update</db:literal> mode compares this schedule to the previous schedule and produces a list of brands that have been added or deleted. <db:literal>update</db:literal> mode requires the <db:tag>previous-schedule</db:tag> parameter to be specified. <db:literal>auto</db:literal> mode attempts to discover if the previous schedule is available; if it is then it switches to <db:literal>update</db:literal> mode, if not then <db:literal>full</db:literal> mode is used.</db:param>
  <xsl:param name='mode' select='"full"'/>

  <db:param>The URL for the previous schedule. This parameter is required for <db:literal>update</db:literal> mode, but is otherwise ignored.</db:param>
  <xsl:param name='previous-schedule'/>
  <xsl:variable name='prev' select='document($previous-schedule)'/>

  <db:param>The dispensing rule to use.</db:param>
  <xsl:param name='dispensing-rule' select='"http://schema.pbs.gov.au/DR/S90"'/>

  <xsl:variable name='dispensing-incentive'
    select='"http://schema.pbs.gov.au/Incentive#dispensing"'/>

  <xsl:key name='pr-by-id'
    match='pbs:prescribing-rule'
    use='pbs:code'/>
  <xsl:key name='ident'
    match='*'
    use='@xml:id'/>

  <xsl:template match='/'>
    <html>
      <head>
        <title>
          <xsl:text>PBS Schedule </xsl:text>
          <xsl:value-of select='/*/pbs:root/pbs:info/terms:valid[1]'/>
        </title>
      </head>
      <body>
        <xsl:call-template name='content'/>
      </body>
    </html>
  </xsl:template>

  <xsl:template name='content'>
    <xsl:variable name='schedule-date-year'
      select='substring(/*/pbs:root/pbs:info/terms:valid[1], 1, 4)'/>
    <xsl:variable name='schedule-date-month'
      select='substring(/*/pbs:root/pbs:info/terms:valid[1], 6, 2)'/>

    <xsl:choose>
      <xsl:when test='$mode = "auto" and
                      ($schedule-date-year = "" or
                      $schedule-date-month ="" or
                      translate($schedule-date-year, "0123456789", "") != "" or
                      translate($schedule-date-month, "0123456789", "") != "")'>
        <p style='color: red;'>auto mode cannot determine schedule date</p>
      </xsl:when>
      <xsl:when test='$mode = "auto"'>
        <xsl:variable name='previous-year'>
          <xsl:choose>
            <xsl:when test='$schedule-date-month = 1'>
              <xsl:value-of select='$schedule-date-year - 1'/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select='$schedule-date-year'/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name='previous-month'>
          <xsl:choose>
            <xsl:when test='$schedule-date-month = 1'>12</xsl:when>
            <xsl:otherwise>
              <xsl:value-of select='format-number($schedule-date-month - 1, "00")'/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
 
        <xsl:variable name='previous-schedule'
          select='concat("G2B ", $previous-year, $previous-month, "01.xml")'/>

        <xsl:variable name='prev' select='document($previous-schedule)'/>

        <xsl:choose>
          <xsl:when test='$prev'>
            <!-- switch to mode "update" -->
            <xsl:call-template name='update'>
              <xsl:with-param name='prev' select='$prev'/>
              <xsl:with-param name='mode'>update</xsl:with-param>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <!-- switch to mode "full" -->
            <xsl:call-template name='full'>
              <xsl:with-param name='mode'>full</xsl:with-param>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>

      <xsl:when test='$mode = "update"'>
        <xsl:call-template name='update'>
          <xsl:with-param name='prev' select='$prev'/>
          <xsl:with-param name='mode'>update</xsl:with-param>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name='full'>
          <xsl:with-param name='mode'>full</xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name='full'>
    <xsl:param name='prev' select='/..'/>
    <xsl:param name='mode' select='"full"'/>

    <table width='100%' border='1'>
      <xsl:call-template name='table-header'/>
      <xsl:apply-templates>
        <xsl:with-param name='other' select='$prev'/>
        <xsl:with-param name='mode' select='"full"'/>
      </xsl:apply-templates>
    </table>
  </xsl:template>
  <xsl:template name='update'>
    <xsl:param name='prev' select='/..'/>
    <xsl:param name='mode' select='"update"'/>

    <xsl:if test='not($prev)'>
      <p style='color: red;'>unable to open previous month's schedule</p>
    </xsl:if>

    <h2>ADDITIONS</h2>

    <!-- First look in the current document (the current schedule).
         For each incentive, see if the corresponding item-brand has the incentive
         in the previous schedule.
      -->

    <table width='100%' border='1'>
      <xsl:call-template name='table-header'/>
      <xsl:apply-templates>
        <xsl:with-param name='other' select='$prev'/>
        <xsl:with-param name='mode' select='"update"'/>
      </xsl:apply-templates>
    </table>

    <h2>DELETIONS</h2>

    <!-- Now look in the previous document (last month's schedule).
         For each incentive, see if the corresponding item-brand has the incentive
         in the current schedule.
      -->

    <table width='100%' border='1'>
      <xsl:call-template name='table-header'/>
      <xsl:apply-templates select='$prev'
        mode='deletions'>
        <xsl:with-param name='other' select='/'/>
        <xsl:with-param name='mode' select='"update"'/>
      </xsl:apply-templates>
    </table>
  </xsl:template>

  <xsl:template match='pbs:prices' mode='deletions'>
    <xsl:param name='other' select='/..'/>

    <xsl:call-template name='prices'>
      <xsl:with-param name='other' select='$other'/>
      <xsl:with-param name='mode' select='"update"'/>
    </xsl:call-template>
  </xsl:template>

  <db:template>
    <db:info>
      <db:title>Prices</db:title>
    </db:info>
    <db:para>This template checks whether a price specifies a dispensing incentive. In <db:literal>full</db:literal> mode, the incentive is displayed. In <db:literal>update</db:literal> mode, the TPP is compared to either the previous schedule (for additions) or current schedule (for deletions) and is only displayed if it is not present in the other document.</db:para>
    <db:para>This template works for both additions and deletions. The only difference is which document is being checked for the corresponding TPP. The document to compare against is given in the <db:literal>other</db:literal> parameter.</db:para>
  </db:template>
  <xsl:template match='pbs:prices' name='prices'>
    <xsl:param name='other' select='/..'/>
    <xsl:param name='mode'/>

    <xsl:if test='pbs:incentives-list[pbs:incentive[pbs:type = $dispensing-incentive]]'>
      <xsl:for-each select='pbs:tpp-reference |
                            pbs:tpp-list/pbs:tpp-reference'>
        <xsl:choose>
          <xsl:when test='$mode = "update"'>
            <!-- remember these values for later when we're inside the for-each -->
            <xsl:variable name='item-code' select='ancestor::pbs:prescribing-rule/pbs:code'/>
            <xsl:variable name='org-code'
              select='key("ident", substring-after(key("ident", substring-after(@xlink:href, "#"))/pbs:organisation-reference/@xlink:href, "#"))/pbs:code'/>

            <xsl:variable name='other-dispensing-incentive'>
              <xsl:for-each select='$other'>
                <xsl:variable name='other.item'
                  select='key("pr-by-id", $item-code)'/>
                <xsl:if test='$other.item'>
                  <xsl:for-each select='$other.item/pbs:prices[pbs:incentives-list/pbs:incentive[pbs:type = $dispensing-incentive]]'>
                    <xsl:for-each select='pbs:tpp-reference |
                                          pbs:tpp-list/pbs:tpp-reference'>
                      <xsl:if test='key("ident", substring-after(key("ident", substring-after(@xlink:href, "#"))/pbs:organisation-reference/@xlink:href, "#"))/pbs:code = $org-code'>
                        <xsl:value-of select='../pbs:incentives-list/pbs:incentive[pbs:type = $dispensing-incentive]/pbs:amount[@dispensing-rule = $dispensing-rule]'/>
                      </xsl:if>
                    </xsl:for-each>
                  </xsl:for-each>
                </xsl:if>
              </xsl:for-each>
            </xsl:variable>

            <xsl:if test='normalize-space($other-dispensing-incentive) = ""'>
              <tr>
                <xsl:call-template name='display'>
                  <xsl:with-param name='what' select='$display'/>
                </xsl:call-template>
              </tr>
            </xsl:if>
          </xsl:when>
          <xsl:otherwise>
            <tr>
              <xsl:call-template name='display'>
                <xsl:with-param name='what' select='$display'/>
              </xsl:call-template>
            </tr>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>

  <db:template name='display'>
    <db:refpurpose>Display values</db:refpurpose>
    <db:para>This template implements a generalised mechanism for displaying values from the data.  The <db:literal>what</db:literal> parameter controls what values are included and in what order.</db:para>
  </db:template>
  <xsl:template name='display'>
    <xsl:param name='what'/>

    <xsl:choose>
      <xsl:when test='normalize-space($what) = ""'/>
      <xsl:when test='contains($what, "&#xa;") and
                      normalize-space(substring-before($what, "&#xa;")) = ""'>
        <xsl:call-template name='display'>
          <xsl:with-param name='what'
            select='substring-after($what, "&#xa;")'/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test='contains($what, "&#xa;")'>
        <xsl:call-template name='display-one'>
          <xsl:with-param name='item'
            select='normalize-space(substring-before($what, "&#xa;"))'/>
        </xsl:call-template>
        <xsl:call-template name='display'>
          <xsl:with-param name='what'
            select='substring-after($what, "&#xa;")'/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name='display-one'>
          <xsl:with-param name='item'
            select='normalize-space($what)'/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <db:template name='display'>
    <db:refpurpose>Display a value</db:refpurpose>
    <db:para>Display a single data value.</db:para>
  </db:template>
  <xsl:template name='display-one'>
    <xsl:param name='item'/>

    <xsl:variable name='pr' select='ancestor::pbs:prescribing-rule'/>

    <xsl:choose>
      <xsl:when test='$item = ""'/>
      <xsl:when test='$item = "drug-name" and
                      $pr/pbs:ready-prepared'>
        <td valign='top'>
          <xsl:apply-templates select='key("ident", substring-after($pr/*/pbs:mpp-reference/@xlink:href, "#"))/../db:title'
            mode='textonly'/>
        </td>
      </xsl:when>
      <xsl:when test='$item = "drug-name" and
                      $pr/pbs:infusible'>
        <td valign='top'>
          <xsl:apply-templates select='key("ident", substring-after($pr/*/pbs:mp-reference/@xlink:href, "#"))/db:title'
            mode='textonly'/>
        </td>
      </xsl:when>
      <xsl:when test='$item = "drug-name" and
                      ($pr/pbs:standard-formula-preparation |
                      $pr/pbs:drug-tariff |
                      $pr/pbs:extemporaneous-preparation)'>
        <td valign='top'>
          <xsl:apply-templates select='$pr/*/db:title'
            mode='textonly'/>
        </td>
      </xsl:when>
      <xsl:when test='$item = "item-code"'>
        <td valign='top'>
          <xsl:apply-templates select='ancestor::pbs:prescribing-rule/pbs:code'
            mode='textonly'/>
        </td>
      </xsl:when>
      <xsl:when test='$item = "org-code"'>
        <td valign='top'>
          <xsl:apply-templates select='key("ident", substring-after(key("ident", substring-after(@xlink:href, "#"))/pbs:organisation-reference/@xlink:href, "#"))/pbs:code'
            mode='textonly'/>
        </td>
      </xsl:when>
      <xsl:when test='$item = "amount"'>
        <td valign='top'>
          <xsl:apply-templates select='../pbs:incentives-list/pbs:incentive[pbs:type = $dispensing-incentive]/pbs:amount[@dispensing-rule = $dispensing-rule]'
            mode='textonly'/>
        </td>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match='*'>
    <xsl:param name='other' select='/..'/>
    <xsl:param name='mode'/>

    <xsl:apply-templates>
      <xsl:with-param name='other' select='$other'/>
      <xsl:with-param name='mode' select='$mode'/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match='*' mode='deletions'>
    <xsl:param name='other' select='/..'/>
    <xsl:param name='mode'/>

    <xsl:apply-templates>
      <xsl:with-param name='other' select='$other'/>
      <xsl:with-param name='mode' select='$mode'/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match='text()|@*'/>
  <xsl:template match='text()|@*' mode='deletions'/>

  <xsl:template name='table-header'>
    <tr>
      <xsl:call-template name='table-header-cell'>
        <xsl:with-param name='values' select='$display'/>
      </xsl:call-template>
    </tr>
  </xsl:template>
  <xsl:template name='table-header-cell'>
    <xsl:param name='values'/>

    <xsl:choose>
      <xsl:when test='not($values)'/>
      <xsl:when test='contains($values, "&#xa;")'>
        <xsl:call-template name='table-header-cell'>
          <xsl:with-param name='values'
            select='substring-before($values, "&#xa;")'/>
        </xsl:call-template>
        <xsl:call-template name='table-header-cell'>
          <xsl:with-param name='values'
            select='substring-after($values, "&#xa;")'/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <th valign='top'>
          <xsl:value-of select='$values'/>
        </th>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
